import { Module } from "vuex";
import { IAnalysisStateType } from "./type";
import { IRootState } from "../type";
import {
  getCategoryGoodsCount,
  getCategoryGoodsSale,
  getCategoryGoodsFavor,
  getSaleGoodsTop10,
  getAddressGoodsSale,
  getAmountGoodsList
} from "@/service/main/analysis/analysis";

const analysisModule: Module<IAnalysisStateType, IRootState> = {
  namespaced: true,
  state() {
    return {
      categoryGoodsCount: [], //每个分类商品的个数
      categoryGoodsSale: [], //每个分类商品的销量
      categoryGoodsFavor: [], //每个分类商品的收藏
      saleGoodsTop10: [], //销量前10的商品数量
      addressGoodsSale: [], //不同城市的销量数据
      amountGoodsList: [] //商品数据统计的数量
    };
  },
  mutations: {
    changeGoodsCount(state, goodsCount) {
      state.categoryGoodsCount = goodsCount;
    },
    changeGoodsSale(state, goodsSale) {
      state.categoryGoodsSale = goodsSale;
    },
    changeGoodsFavor(state, goodsFavor) {
      state.categoryGoodsFavor = goodsFavor;
    },
    changeGoodsTop10(state, goodsTop) {
      state.saleGoodsTop10 = goodsTop;
    },
    changeAddressSale(state, addressSale) {
      state.addressGoodsSale = addressSale;
    },
    changeAmountList(state, amount) {
      state.amountGoodsList = amount;
    }
  },
  actions: {
    async getDashboardPageDataAction({ commit }) {
      const categoryGoodsCount = await getCategoryGoodsCount();
      commit("changeGoodsCount", categoryGoodsCount.data);

      const categoryGoodsSale = await getCategoryGoodsSale();
      commit("changeGoodsSale", categoryGoodsSale.data);

      const categoryGoodsFavor = await getCategoryGoodsFavor();
      commit("changeGoodsFavor", categoryGoodsFavor.data);

      const saleGoodsTop10 = await getSaleGoodsTop10();
      commit("changeGoodsTop10", saleGoodsTop10.data);

      const addressGoodsSale = await getAddressGoodsSale();
      commit("changeAddressSale", addressGoodsSale.data);

      const amountGoodsList = await getAmountGoodsList();
      commit("changeAmountList", amountGoodsList.data);
    }
  }
};

export default analysisModule;
